<template>
	<view class="dataTest">
		<!-- 结果统计 -->
		<view class="result flex-space-between bb pb20">
			<view class="flex-derection-c-center">
				<view class="grey-tip">总次数</view>
				<view class="stress">{{logArr.length}}</view>
			</view>
			<view class="flex-derection-c-center">
				<text class="grey-tip">成功数</text>
				<text class="success">{{getSuccessCount}}</text>
			</view>
			<view class="flex-derection-c-center">
				<view class="grey-tip">失败数</view>
				<view class="warn">{{getFailCount}}</view>
			</view>
		</view>
		<!-- 结果统计 -->
		<!-- 测试选择 -->
		<view class="mt20">
			<radio-group @change="radioChange" class="flex-h-c ">
				<label v-for="(item, index) in items" :key="item.value" class="flex-h-c mr20">
					<view>
						<radio :value="item.value" :disabled="disabled" :checked="index === current" color="#1F95EC"
							style="transform:scale(0.7)" />
					</view>
					<view>{{item.name}}</view>
				</label>
			</radio-group>
		</view>
		<!-- 测试选择 -->
		<!-- 发送数据 -->
		<view class="flex-space-between mt20" v-if="current==0">
			<textarea class="border p10" v-model="sendData"></textarea>
			<view class="pl20">
				<view class="mb20">
					<xc-button name="发送" @click="sendDataFun" :disabled="disabled"></xc-button>
				</view>
				<xc-button name="重置" type="minor_btn" @click="resetData" :disabled="disabled"></xc-button>
			</view>
		</view>
		<!-- 发送数据 -->
		<!-- 自动化测试配置 -->
		<view class="mt20 bb mb20" v-if="current==1">
			<view class="flex mb20">
				<text>发送间隔:</text><input class="border testInput" type="number" v-model="sendTimeout" /><text>ms</text>
			</view>
			<view class="flex mb20">
				<text>发送次数:</text><input class="border testInput" type="number" v-model="sendCount" /><text>次</text>
			</view>
			<view class="flex mb20">
				<text>发送长度:</text><input class="border testInput" type="number" v-model="sendLen" /><text>字符</text>
			</view>
			<!-- 发送按钮 -->
			<view class="zd_button flex mb20 mt40">
				<xc-button name="发送" @click="sendDataFun" :disabled="disabled"></xc-button>
				<view class="pl20">
					<xc-button name="重置" type="minor_btn" @click="resetData" :disabled="disabled"></xc-button>
				</view>


			</view>
			<!-- 发送按钮 -->
		</view>

		<!-- 自动化测试配置 -->
		<!-- 日志数据 -->
		<view class="mt20">
			<view class="primary_color flex-space-between mb10">
				<text>日志内容</text>
				<view>
					<xc-button name="清除日志" type="minor_btn" @click="resetLog"></xc-button>
				</view>
			</view>
			<view class="p10 log-wrapper border">
				<view class="mb20 bbdash" v-for="(item,index) in showLogArr">
					<view class="mb10">
						<view class="grey-tip">发送({{item.sendData.time}}):</view>
						<view class="word-wrap">
							{{item.sendData.data}}
						</view>
					</view>
					<view class=" mb10">
						<view class="grey-tip">接收({{item.receiveData.time}})：</view>
						<view :class="[item.isSuccess ? '' : 'warn','word-wrap']">
							{{item.receiveData.data}}
						</view>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import xcButton from "@/components/simple/xcButton.vue"
	import bluetoothTools from "@/static/utils/bluetoothTools.js"
	import {
		throttle,
		getNowTime
	} from "@/static/utils/utils.js"
	export default {
		data() {
			return {
				sendTimeout: '',
				sendCount: '',
				sendLen: '',
				sendData: "",
				count: 1,
				disabled: false,
				logArr: [],
				showLogArr:[],
				callbackCount: 0,
				sendDataArr: [],
				items: [{
						value: '0',
						name: '自定义测试'
					},
					{
						value: '1',
						name: '自动化测试'
					}
				],
				current: 0
			}
		},
		components: {
			xcButton
		},
		computed: {

			getSuccessCount() {
				return this.getPercentage();

			},
			getFailCount() {
				return this.getPercentage(false);
			},
		},
		methods: {
			getPercentage(isSuccess = true) {
				let result = '0';
				let len = this.logArr.length;
				if (len > 0) {
					let arr = this.logArr.filter(item => {
						return item.isSuccess == isSuccess;
					})
					let percentage = (arr.length / len).toFixed(2) * 100;
					return arr.length + '(' + percentage + '%)';
				} else {
					return 0;
				}


			},
			radioChange: function(evt) {
				this.resetData();
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value === evt.target.value) {
						this.current = i;
						break;
					}
				}
			},
			//清除日志
			resetLog(){
				this.showLogArr = [];
			},
			//重置数据
			resetData() {
				this.sendTimeout = '';
				this.sendCount = '';
				this.sendLen = '';
				this.sendData = "";
				this.disabled = false;
				this.logArr = [];
				this.showLogArr = [];
			},
			validData() {
				if (this.current == 0) {
					let rg = /^[A-Fa-f0-9]+$/;
					if (this.sendData == "") {
						this.$showModal("请输入发送数据");
						return false;
					} else if (!rg.test(this.sendData)) {
						this.$showModal("请输入16进制");
						return false;
					} else {
						return true;
					}
				} else {
					if (this.sendTimeout == "" && parseInt(this.sendTimeout) > 0) {
						this.$showModal("请输入大于0的间隔时间");
						return false;
					}

					if (this.sendCount == "" && parseInt(this.sendCount) > 0) {
						this.$showModal("请输入大于0的发送次数");
						return false;
					}

					if (this.sendLen == "" && parseInt(this.sendLen) > 0) {
						this.$showModal("请输入大于0的发送长度");
						return false;
					}
					return true;
				}
			},

			//发送
			sendDataFun: throttle(function() {
				let flag = this.validData();
				if (!flag) return;
				this.disabled = true;
				this.sendDataArr = [];
				this.callbackCount = 0;
				if (this.current == 0) {
					this.sendDataArr.push({
						time: getNowTime(),
						data: this.sendData,
					})
					bluetoothTools.directWriteCard("writeCallback", this.sendData, this);
				} else {
					this.count = 1;
					if (this.sendCount == 1) {
						this.sendDataByAuto();
					} else if (this.sendCount > 1) {
						this.sendDataByAuto();
						let timer = setInterval(() => {
							console.log(this.sendCount + "======" + this.count)
							this.sendDataByAuto();
							this.count++;
							if (this.sendCount == this.count) {
								clearInterval(timer);
								this.disabled = false;
							}
						}, this.sendTimeout);
					}
				}
			}),

			sendDataByAuto() {
				let sendData = this.getDataByLen(this.sendLen);
				console.log("生成的sendData数据", sendData)
				this.sendDataArr.push({
					time: getNowTime(),
					data: sendData,
				})

				//this.writeCallback(sendData)
				bluetoothTools.directWriteCard("writeCallback", sendData, this);
			},
			getDataByLen(len) {
				var result = '';
				for (var i = 0; i < len; i++) {
					result += Math.floor(Math.random() * 16).toString(16); //获取0-15并通过toString转16进制
				}
				//默认字母小写，手动转大写
				return result.toUpperCase(); //另toLowerCase()转小写	
			},
			writeCallback(relust) {
				if (this.current == '0' || this.sendCount == '1') {
					this.disabled = false;
				}
				let isSuccess = true;
				let sendData = this.sendDataArr[this.callbackCount].data;
				if (relust.toUpperCase() != sendData) {
					isSuccess = false;
				}

				let logData = {
					sendData: this.sendDataArr[this.callbackCount],
					isSuccess: isSuccess,
					receiveData: {
						time: getNowTime(),
						data: relust
					}
				}
				this.callbackCount++;

				this.logArr.push(logData);
				
				this.showLogArr.push(logData);
			}

		},


	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.dataTest {
		.log-wrapper {
			max-height: 600upx;
			overflow: auto;
			min-height: 200upx;
		}

		.zd_button {
			justify-content: center;
		}

		uni-textarea {
			height: 200upx !important;
		}

		.testInput {
			padding: 10upx 16upx;
			margin-right: 6upx;
		}

		padding: 30upx;

		.xc_btn {
			height: 80upx;
			line-height: 80upx;
			font-size: 30upx;
			letter-spacing: 1upx;
			padding: 0;
			width: 140upx;
		}
	}
</style>
